package com.raulraja.service.impl;

import com.raulraja.dto.PersonDTO;
import com.raulraja.model.domain.impl.Person;
import com.raulraja.model.manager.PersonManager;
import com.raulraja.service.PersonBusinessService;
import com.raulraja.service.exceptions.PersonNotActiveException;

/**
 * Default impl of the PersonBusinessService
 */
public class PersonBusinessServiceImpl implements PersonBusinessService {

	/**
	 * the manager that this service uses to lookup entities on the data store
	 */
	private PersonManager personManager;

	/**
	 * Sets the person manager
	 * @param personManager the manager that this service uses to lookup entities on the data store
	 */
	public void setPersonManager(PersonManager personManager) {
		this.personManager = personManager;
	}

	/**
	 * Finds an active person based on the dto data
	 *
	 * @param personDTO the person dto conataining the info necessary to find a person
	 * @return the found person
	 */
	public Person getActivePerson(PersonDTO personDTO) {
		Person person = personManager.find(personDTO.getId());
		if (person != null) {
			if (!person.isActive()) {
				throw new PersonNotActiveException(person + " is not active");
			}
		} else {
			throw new IllegalArgumentException("No person found with id: " + personDTO.getId());
		}
		return person;
	}
}
